<template>
	<view>
		<view class="d-waikuang-10">
			
			<u-sticky offset-top="0" bgColor="#ffffff">
				<!--导航-->
				<u-tabs-d 
					:list="daohang"
					@click="click" 
					:lineColor="quan.beijingyanse"
					:activeStyle="{
							color: '#000000',
							fontWeight: 'bold',
							transform: 'scale(1.05)'
						}"
				></u-tabs-d>
				<!--二级导航-->
				<u-tabs-d
					v-if="daohangerjixs && qid != 0"
					:list="daohangerji" 
					@click="clickErji" 
					:lineColor="quan.beijingyanse"
					:activeStyle="{
							color: '#000000',
							fontWeight: 'bold',
							transform: 'scale(1.05)'
						}"
				></u-tabs-d>
			</u-sticky>
			<view class=" d-shang-10">
				<uni-forms
					:modelValue="formData" 
					label-position="top"
				>
					<u--input
						placeholder=" ( 非选项 ) 请输入标题"
						border="bottom"
						v-model="tietitle" 
						placeholder-style="color:#b8b8b8;font-size:32rpx;text-align:center;"
						clearable
					  ></u--input>
					<view class="d-kuan d-shang-10">
						<view class="d-beijing-hui">
							<uni-easyinput 
							    @input="descInput($event)"
								type="textarea" 
								autoHeight 
								v-model="neirong" 
								placeholder="( *必须 ) 请输入内容" 
								placeholder-style="color:#b8b8b8;font-size:32rpx;text-align:center;"
								placeholder-class="place-holder"
								:inputBorder="false"
								:border="true"
								maxlength="-1"
							>
							</uni-easyinput>
						</view>
					</view>
					<view class="d-kuan d-shang-10">
						<view class="example-body">
							<u-upload
								:fileList="fileList1"
								@afterRead="afterRead"
								@delete="deletePic"
								name="1"
								multiple
								:maxCount="9"
							></u-upload>
						</view>
					</view>
				</uni-forms>
			</view>
			
		</view>
		
		<view class="goods-carts">
			<view class="d-waikuang-10">
				<view class="d-kuan d-beijing-bai d-yuanjiao-10 ">
					<view class="d-neikuang-zuoyou-6-shangxia-10">
			    		<u-button text="发 布" form-type="submit" @click="postTiezipanduan" shape="circle" color="linear-gradient(to right, rgb(67, 185, 185), rgb(168, 46, 208))"></u-button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import config from "../../utils/config";
import fabudata from "../../utils/configfabu";
var Api = require("../../utils/api.js");
var util = require("../../utils/util.js");

var pageCount = config.getPageCount;
var wxApi = require("../../utils/wxApi.js");
var wxRequest = require("../../utils/wxRequest.js");
var webSiteName = config.getWebsiteName;
var domain = config.getDomain;
var banbenhao = config.getDuixinbanbenhao;
var jiugetudata = fabudata.postDUIXIN_JIUGETU_ID;

export default {
	data() {
		return {
			imagejiugetu:[],
			show: true,
			formData: {},
			quanid:'',
			quan: [],
			daohang: [],
			daohangerji: [],
			daohangerjixs:false,
			daohangid: '0',
			title:'',
			userInfo:'',
			keycode:'',
			uid:'',
			qid:'',
			columns:[],
			columnData:[],
			code:'',
			fileList1: [],
			uptu:[],
			jgtkgto: false,
			catid:'',
			catidErji:'',
			yiji:'',
			erji:'',
			eErjiid:'',
			tietitle:'',
			neirong:'',
			neirongpp:'',
			tid:'',
			yixuanzeErjis:false,
			jiugetukaiguan:'',
			fabuid:{}
		};
	},
	onLoad: function (options) {
		var self = this;
		var duixin = this;
		self.quanid = options.id;
		this.getQuan(options.id);
		duixin.getFabuid();
		var userInfo = uni.getStorageSync('userInfo');
		self.code = userInfo;
		var keycode = uni.getStorageSync('keycode');
		self.keycode = keycode;
		

		self.userInfo = userInfo;
		if (userInfo) {
			self.showuid = true;
			var member = uni.getStorageSync('member');
			var uid = uni.getStorageSync('memberuid');
			self.uid = uid;

		} else {
			//否则跳转到登录页面
			uni.navigateTo({
				url: '/pages/login/login'
			});
			
		}
		self.getShiming();
		self.getDangqianurl();//获取当前网址
	},
	methods: {
		// 获取圈子信息
		getQuan(quanid) {
			var self = this;
			self.setData({
			  quan: [],
			  daohang: [],
			  columns:[]
			});
			var getQuanRequest = wxRequest.getRequest(Api.getQuan(quanid,self.qid,self.erji)+'&fabu=1');
			getQuanRequest.then(response => {
				if (response.data.code == 1) {
					
				  //console.log(response.data.data); 
				  self.quan = response.data.data;
				  
				  self.title = response.data.data.title; //beijingyanse
				  self.getDaohangerj(quanid);
				  
				  self.daohang = response.data.data.daohang; 
				  /*var columns = response.data.data.daohang;*/
				  //self.columns = columns;
				  /*for (let key= 0; key < columns.length; key++) {
					  self.columns[key] = {
						  id: columns[key].id,
						  name: columns[key].name
					  };
					  //self.columns[key].id = columns[key].name;
					  //self.columns[key].name = columns[key].name;
					  console.log('重复',columns[key]);
					}
					console.log('一级',self.columns);*/
					
				  var beijingyanse = response.data.data.beijingyanse;
				  uni.setNavigationBarTitle({
				    title: self.title
				  });
				  //uni.setNavigationBarColor("#ffffff", beijingyanse);
				  uni.setNavigationBarColor({
				      frontColor: '#ffffff',
				      backgroundColor: beijingyanse,
				      animation: {
				          duration: 400,
				          timingFunc: 'easeIn'
				      }
				  });
				  
		
				} else {
				  self.setData({
					displaySwiper: "none"
				  });
				}
			});
		},
		//二级导航
		getDaohangerj(quanid) {
			var self = this;
			
			var getQuanRequest = wxRequest.getRequest(Api.getQuan(quanid,self.qid,self.erji)+'&fabu=1');
			getQuanRequest.then(response => {
				if (response.data.code == 1) {
					
				  if (response.data.data.daohangerji) {
				  		
						if(response.data.data.daohangerji && response.data.data.daohangerji.length){
							self.yixuanzeErjis = true;
							self.daohangerjixs = true;
						} else {
							self.yixuanzeErjis = false;
							self.daohangerjixs = false;
						}
						
				  		self.daohangerji = response.data.data.daohangerji;
						self.columnData = response.data.data.daohangerji;
						self.yiji == 1;
						//console.log('yixuanzeErjis',self.yixuanzeErjis);
				  } else {
				  		self.daohangerjixs = false;
						self.yixuanzeErjis = false;
						self.yiji == 0;
						//console.log('yixuanzeErjis',self.yixuanzeErjis);
				  }
				  
				} else {
				  self.setData({
					displaySwiper: "none"
				  });
				}
			});
		},
		//一级栏目
		click(payload){
			var duixin = this;
			duixin.qid = payload.id
			duixin.erji = '';
			duixin.getDaohangerj(duixin.quanid);
			
			//duixin.quanidurl();
			//缓存一级id
			uni.setStorageSync('yijiid', payload.id);
			var yijiid = uni.getStorageSync('yijiid');
			duixin.yijiid = yijiid;
			duixin.daohangid = yijiid;
			duixin.catid = yijiid;
			duixin.tid = yijiid;
			//console.log('tid栏目一级',duixin.tid);
			/*catidErji:'',
			child:'',
			eErjiid:'',*/
			
			//清理二级id
			uni.removeStorageSync('erjiid');
			//console.log('yijiid',yijiid); 
		},
		//二级栏目
		clickErji(payload) {
			var duixin = this;
			
			//一级id
			duixin.qid = duixin.yijiid;
			//console.log('一级导航',duixin.qid);
			duixin.erji = payload.eid;
			duixin.tid = payload.eid;
			//console.log('tid二级导航',payload.eid);
			duixin.daohangid = payload.eid;
		},
		changeHandler(e) {
			const {
				columnIndex,
				value,
				values, // values为当前变化列的数组内容
				index,
				// 微信小程序无法将picker实例传出来，只能通过ref操作
				picker = this.$refs.uPicker
			} = e
			// 当第一列值发生变化时，变化第二列(后一列)对应的选项
			if (columnIndex === 0) {
				// picker为选择器this实例，变化第二列对应的选项
				picker.setColumnValues(1, this.columnData[index])
			}
		},
		// 回调参数为包含columnIndex、value、values
		confirm(e) {
			//console.log('confirm', e)
			this.show = false
		},
		//上传jiugetu
		D_jiugetu(e) {
			var self = this; 
			var uid = self.uid;
			var code = self.code;
			//console.log("上code",e);
			uni.uploadFile({
			  url: Api.postTupians(uid,code), //
			  filePath: e.tempFiles[0].url,
			  name: 'file_data',
			  formData: {
				is_ajax:1
			  },
			header: {
				'Content-Type': 'application/x-www-form-urlencoded',
			},
			  success (res){
				//const data = res.data
				//do something
				uni.showToast({
					title: "上传成功",
					icon: 'success',
				});
			  }
			})
			//console.log("下",e);
			/*success: function (res) {
				//console.log("图片",res.tempFiles[0].tempFilePath) // 打印错误信息
				
			}*/
			/*uni.chooseMedia({
			    count: 1,
			    mediaType: ['image'],
			    sourceType: ['album', 'camera'],
			    maxDuration: 30,
			    camera: 'back', //yonghuXinxi.uid,yonghuXinxi.auth
			    
			})*/
		},
		//上传多图接口
		// 删除图片
		deletePic(event) {
			this[`fileList${event.name}`].splice(event.index, 1)
		},
		// 新增图片
		async afterRead(event) {
			var duixin = this; 
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			console.log('event',event); 
			let lists = [].concat(event.file)
			let fileListLen = this[`fileList${event.name}`].length
			lists.map((item) => {
				this[`fileList${event.name}`].push({
					...item,
					status: 'uploading',
					message: '上传中'
				})
			})
			for (let i = 0; i < lists.length; i++) {
				const result = await this.uploadFilePromise(lists[i].url)
				const id = duixin.uptu.data.id;
				console.log('uptu',duixin.uptu.data.id); 
				let item = this[`fileList${event.name}`][fileListLen]
				this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
					status: 'success',
					message: '',
					url: result,
					id:id
				}))
				fileListLen++
				
			}
		},
		//发布id 
		getFabuid(){
			
			var duixin = this;
			
			var getFabuidRequest = wxRequest.getRequest(Api.getFabuid());
			getFabuidRequest.then(response => {
				if (response.data.code == 1) {
					
				  duixin.fabuid = response.data.data;
				  
				} else {
				  duixin.setData({
					displaySwiper: "none"
				  });
				}
			});
		},
		uploadFilePromise(url) {
			
			var self = this; 
			var duixin = this; 
			console.log('fabuidjiugetu',duixin.fabuid && duixin.fabuid.jiugetu); 
			var uid = self.uid;
			var code = self.code;
			var jiugetuid = duixin.fabuid && duixin.fabuid.jiugetu;
			return new Promise((resolve, reject) => {
				let a = uni.uploadFile({
					
					url: Api.postTupians(uid,code)+'&fid='+jiugetuid, //
					filePath: url,
					name: 'file_data',
					formData: {
						is_ajax:1
					},
					success: (res) => {
						var duixin = this; 
						duixin.uptu = JSON.parse(res.data);
						setTimeout(() => {
							resolve(res.data)
						}, 1000)
						
						//上传图片成功后，打开九格图的开关
						duixin.jiugetukaiguan = 1;
						//console.log('图片开关返回报错：',duixin.jiugetukaiguan);
						}
				});
			})
		},
		//提交判断
		postTiezipanduan(){
			var duixin = this;
			var catid = duixin.catid;
			if(catid > 0) {
				var yixuanzeErjis = duixin.yixuanzeErjis;
				
				var erji = duixin.erji;
				var neirong = duixin.neirongpp;
				if(yixuanzeErjis) {
					
					if(erji) {
						if(!neirong){
							uni.showToast({
								icon: 'none',
								title: '内容不能为空',
								duration: 5000
							});
						} else {
							duixin.postTiezi();
						}
						
					} else {
						uni.showToast({
							icon: 'none',
							title: '请选择二级栏目',
							duration: 5000
						});
					}
					
				} else {
					if(!neirong){
						uni.showToast({
							icon: 'none',
							title: '内容不能为空',
							duration: 5000
						});
					} else {
						duixin.postTiezi();
					}
				}
			} else {
				uni.showToast({
					icon: 'none',
					title: '请选择栏目',
					duration: 5000
				});
			}
		},
		//提交帖子
		postTiezi() {
			var self = this;
			self.setData({
			  data: []
			});
			var title = self.tietitle;
			
			
			var yixuanzeErjis = self.yixuanzeErjis;
			var erji = self.erji;
			
			var neirong = self.neirongpp;
			
			var catid = self.catid;
			var yiji = self.yiji;
			var tid = self.tid; // 栏目id
			
			var jiugetu =self.fileList1;
			// 九格图开关 原来值：jgtkg 已经废除
			if(self.jiugetukaiguan == 1){
				var jgtkg = 1;
			} else {
				var jgtkg = 0;
			}
			
			//console.log('九格图是否开启',jgtkg);
		
			let img = '';
			jiugetu.forEach(item=>{
				img+= '&data[jiugetu][id][]='+item.id+'&data[jiugetu][title][]=tupian'
			})
			//console.log('img',img);
			//console.log('datatus',datatus);
			var yixuanzeErji = self.yixuanzeErjis;
			//console.log('yixuanzeErji',yixuanzeErji);
			if(catid > 0) {
				var datas = "is_ajax=1&data[title]="+title+"&data[neirong]="+neirong+"&cid="+self.quanid+img+"&data[jgtkg]="+jgtkg+"&data[yuedumoshi]=1&data[tid]="+tid;
				var postTieziRequest = wxRequest.postRequest(Api.postTiezi(self.uid,self.code,self.quanid),datas);
					postTieziRequest.then(response => {
						if (response.data.code == 1) {
						  //console.log(response.data.data);
						  self.data = response.data.auth;
						    // 保存用户信息到本地storage
						    uni.showToast({
						    	title: response.data.msg,
						    	icon: 'success',
						    	duration: 5000
						    });
				
							var tieid = response.data.data.id;
							self.title = '';
							self.neirong = '';
							self.catid = 0;
							if(response.data.data && response.data.data.data.shenhe == 1) {
								uni.reLaunch({ url: "/pages/tiezi/tiezi?id=" + tieid });
							} else {
								uni.reLaunch({ url: "/pages/quanzi/shenhe" });
							}
							//发布成功后跳转到帖子页面
							//console.log("jgtkg：",jgtkg);
							
						} else {
						  //self.setData({
							//displaySwiper: "none"
						  //});
						  if(catid==0 ) {
						  	uni.showToast({
						  		icon: 'none',
						  		title: '请选择栏目',
						  		duration: 5000
						  	});
						  } else {
							
							uni.showToast({
								icon: 'none',
								title: response.data.msg,
							});
							if(response.data.msg =="账号必须实名认证"){
								uni.showModal({
									title: '请先实名认证',
									//content: '请先登录',
									confirmText: '前往认证',
									success: function(res) {
										if (res.confirm) {
											uni.navigateTo({
												url: '/pages/shezhi/renzheng'
											});
										}
									}
								});
							}
						  }
						  //console.log("jgtkg：",jgtkg);
						  
						}
					});
				/*if(yixuanzeErji){
					var datas = "is_ajax=1&data[title]="+title+"&data[neirong]="+neirong+"&cid="+self.quanid+img+"&data[jgtkg]="+jgtkg+"&data[tid]="+tid;
					var postTieziRequest = wxRequest.postRequest(Api.postTiezi(self.uid,self.code,self.quanid),datas);
						postTieziRequest.then(response => {
							if (response.data.code == 1) {
							  //console.log(response.data.data);
							  self.data = response.data.auth;
							    // 保存用户信息到本地storage
							    uni.showToast({
							    	title: response.data.msg,
							    	icon: 'success',
							    	duration: 5000
							    });

								var tieid = response.data.data[0].id;
								self.title = '';
								self.neirong = '';
								self.catid = 0;
								if(response.data.data[1].shenhe == 1) {
									uni.reLaunch({ url: "/pages/tiezi/tiezi?id=" + tieid });
								} else {
									uni.reLaunch({ url: "/pages/quan/shenhe" });
								}
								//发布成功后跳转到帖子页面
								
								
							} else {
							  //self.setData({
								//displaySwiper: "none"
							  //});
							  if(catid==0 ) {
							  	uni.showToast({
							  		icon: 'none',
							  		title: '请选择栏目',
							  		duration: 5000
							  	});
							  } else {
								uni.showToast({
									icon: 'none',
									title: response.data.msg,
								});
							  }
							  
							  
							}
						});
					
				} else {
					if(erji){
						uni.showToast({
							icon: 'none',
							title: '请选择二级栏目',
							duration: 2000
						});
					}
					console.log('erji',erji);
				}*/
			  
			} else {
				uni.showToast({
					icon: 'none',
					title: '请选择栏目',
					duration: 2000
				});
			}
			
		
		},
		descInput(e){
			var duixin = this; 
		   let newStr = '<p>' + e + '</p>';
		   let content = newStr.replace(/\n/g,"</p><p>");
		   duixin.neirongpp = content;
		   //console.log(duixin.neirongpp)
		},
		//发布实名认证
		getShiming(){
			var duixin = this;
			//console.log('111111111',duixin.uid);
			var getShimingfabuRequest = wxRequest.getRequest(Api.getShimingfabu(duixin.uid,duixin.keycode));
			var res;
			getShimingfabuRequest.then(response => {
				res = response;
				if(res.data.code==1) {
					//console.log('111111111',duixin.uid);
				} else {
					if(res.data.data.zhuangtai == 0){
						uni.showModal({
							title: res.data.msg,
							//content: '请先登录',
							confirmText: '前往认证',
							success: function(res) {
								if (res.confirm) {
									uni.redirectTo({
										url: '/pages/shezhi/renzheng'
									});
								}
							}
						});
					} else {
						
					}
				}
			});
		},
		//获取当前页面
		getDangqianurl() {
			//获取当前页面栈的实例
			let pages = getCurrentPages();
			//获取当前页面
			let currentPage = pages[pages.length-1].$page.fullPath;
			let url = currentPage;
			uni.setStorageSync('dangqianUrl', url);
		},
	}
}
</script>

<style>
	
	/* #ifdef H5 */
	::v-deep .uni-textarea-textarea {
	    resize: none;
	    background: none;
	    color: inherit;
	    opacity: 1;
	    font: inherit;
	    /* line-height: inherit; */
	    letter-spacing: inherit;
	    text-align: inherit;
	    text-indent: inherit;
	    text-transform: inherit;
	    text-shadow: inherit;
	    font-size: 18px;
	    line-height: 30px;
	}
	/* #endif */
	.uni-textarea-textarea {
	    resize: none;
	    background: none;
	    color: inherit;
	    opacity: 1;
	    font: inherit;
	    /* line-height: inherit; */
	    letter-spacing: inherit;
	    text-align: inherit;
	    text-indent: inherit;
	    text-transform: inherit;
	    text-shadow: inherit;
	    font-size: 18px;
	    line-height: 30px;
	}
	.place-holder {
		color:#b8b8b8;font-size:32rpx;text-align:center;
	}
</style>